<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>oxipng</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="oxipng.js"></script>
    <link rel="stylesheet" href="main.css">
</head>

<body>

    <a href="../">home</a>
    <h1>oxipng demo</h1>
    <h4>Input picture format:jpeg、jpg、png、gif、webp、svg、ico、bmp、avif。Multiple files can be selected</h4>
    <input type="file" id="files" accept=".jpeg,.jpg,.png,.gif,.webp,.svg,.ico,.bmp,.avif" multiple />
    <div id="outBox"></div>
    <div id="loading">Converting...</div>



    <script type='module'>
        // import oxipng from './oxipng.js';
        // console.log(oxipng('/oxipng_bg.wasm').then(d=>{

        // console.log(start);

        // }));
    </script>





    <script>
        function imgConver(file) {
            return new Promise((resolve, reject) => {
                let _canvas = document.createElement("canvas");
                let _img = new Image();
                _img.src = window.URL.createObjectURL(file);
                _img.onload = function () {
                    _canvas.setAttribute("width", this.width);
                    _canvas.setAttribute("height", this.height);
                    _canvas.getContext("2d").drawImage(this, 0, 0);
                    let data = _canvas
                        .getContext("2d")
                        .getImageData(0, 0, this.width, this.height);
                    let start = opxFun()

                    opxFun().init('./oxipng_bg.wasm').then(async (d) => {
                        console.log(start.optimise(new Uint8Array(await new Response(file)
                            .arrayBuffer()), 2).buffer);

                    })



                    // mozjpeg_enc().then(mozjpeg_enc_wasm => {
                    //     let avifImg = mozjpeg_enc_wasm.encode(
                    //         data.data,
                    //         this.width,
                    //         this.height, {
                    //             quality: 75,
                    //             baseline: false,
                    //             arithmetic: false,
                    //             progressive: true,
                    //             optimize_coding: true,
                    //             smoothing: 0,
                    //             color_space: 3,
                    //             quant_table: 3,
                    //             trellis_multipass: false,
                    //             trellis_opt_zero: false,
                    //             trellis_opt_table: false,
                    //             trellis_loops: 1,
                    //             auto_subsample: true,
                    //             chroma_subsample: 2,
                    //             separate_chroma_quality: false,
                    //             chroma_quality: 75,
                    //         }
                    //     );
                    //     let outImgBlob = new Blob([avifImg.buffer], {
                    //         type: "image/avif"
                    //     });
                    //     resolve({
                    //         url: URL.createObjectURL(outImgBlob),
                    //         size: (file.size / 1024).toFixed(1) + ' Kb -> ' +
                    //             (outImgBlob.size / 1024).toFixed(1) + ' Kb'
                    //     });
                    // });

                };
            });
        }

        let input = document.getElementById("files");
        let outBox = document.getElementById("outBox");
        let loading = document.getElementById("loading");
        let outData = 0
        let intData = 0
        input.addEventListener("change", function () {
            document.getElementById('loading').style.display = 'block'
            let startTime = new Date().getTime()

            for (const key in this.files) {
                if (this.files.hasOwnProperty(key)) {
                    intData++
                    imgConver(this.files[key]).then(outImg => {
                        outBox.insertAdjacentHTML('beforeend',
                            `<li><div><img src="${outImg.url}"></div><p>${outImg.size}</p></li>`);
                        outData++
                        if (outData === intData) {
                            let jjj = (new Date().getTime() - startTime) / 1000
                            outBox.insertAdjacentHTML('beforeend',
                                `<br/><h1>${jjj}秒</h1>`
                            );
                            document.getElementById('loading').style.display =
                                'none'
                        }
                    })
                }
            }
        }, false);
    </script>
</body>

</html>